﻿using System;
using System.Configuration;

namespace MSL.DataBase.Lib
{
    /// <summary>
    /// 读取配置项
    /// </summary>
    public class Config
    {
        #region 是否存在

        /// <summary>
        /// 配置项是否存在
        /// AppSettings ConnectionStrings
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static bool IsExistApp(string key)
        {
            bool returnValue = false;
            if (ConfigurationManager.ConnectionStrings[key] == null && ConfigurationManager.AppSettings[key] == null)
            {
                returnValue = true;
            }
            return returnValue;
        }
        #endregion

        #region 获取AppSettings

        /// <summary>
        /// 获取配置文件中的字符串 使用AppSettings
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string GetApp(string key)
        {
            return ConfigurationManager.AppSettings[key];
        }
        #endregion

        #region 获取ConnectionStrings

        /// <summary>
        /// 获取配置文件中的字符串 并缓存 使用ConnectionStrings
        /// </summary>
        /// <param name="key"></param>
        /// <param name="providerName">返回数据库类型</param>
        /// <returns></returns>
        public static string GetProviderApp(string key, out string providerName)
        {
            providerName = ConfigurationManager.ConnectionStrings[key].ProviderName;
            object obj = ConfigurationManager.ConnectionStrings[key];
            return obj == null ? String.Empty : obj.ToString();
        }

        #endregion

    }
}
